# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=tools
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}-git")
replaces=("${_mingw_suff}-${_realname}")
provides=("${_mingw_suff}-${_realname}")
conflicts=("${_mingw_suff}-${_realname}")
pkgdesc="MinGW-w64 headers for cross-compiler"
pkgver=7.0.0.5479.8db8dd5a
pkgrel=1
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
makedepends=('git')
options=('!strip' '!libtool' '!emptydirs' '!buildflags')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64")
sha256sums=('SKIP')
_tools="gendef genlib genidl genpeimg" #widl

_targets="i686-w64-mingw32 x86_64-w64-mingw32" #armv7-w64-mingw32

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/mingw-w64"
}

build() {
  for _target in ${_targets}; do
    msg "Configuring ${_target} tools"
    for cur in ${_tools}; do
      mkdir -p ${srcdir}/${cur}-${_target} && cd ${srcdir}/${cur}-${_target}
      ${srcdir}/mingw-w64/mingw-w64-tools/${cur}/configure \
        --build=${CHOST} \
        --host=${_target} \
        --prefix=/opt/${_target}
      
      make
    done
  done
}

package() {
  for _target in ${_targets}; do
    msg "Installing ${_target} tools"
    for cur in ${_tools}; do
      cd ${srcdir}/${cur}-${_target}
      make DESTDIR=${pkgdir} install
    done
  done
}
